package net.csdn.business.mirror.service.impl;

import lombok.extern.slf4j.Slf4j;
import net.csdn.business.mirror.common.Constants;
import net.csdn.business.mirror.entity.bean.ProjectMirrorData;
import net.csdn.business.mirror.entity.bo.MirrorMqBodyBO;
import net.csdn.business.mirror.service.MqProjectChangeImplement;
import net.csdn.business.mirror.service.ProjectMirrorDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Slf4j
@Service
public class MqProjectDeleteImplement implements MqProjectChangeImplement {
    @Autowired
    private ProjectMirrorDataService projectMirrorDataService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void exec(MirrorMqBodyBO bodyBO) {
        // 删除
        ProjectMirrorData data = projectMirrorDataService.getByFullPath(bodyBO.getPath());
        if (Objects.isNull(data)) {
            return;
        }
        projectMirrorDataService.delete(data);
        Long id=data.getId();
        //删除队列中已存在的此值
        try {
            stringRedisTemplate.opsForList().remove(Constants.MIRROR_IMPORT_KEY, Integer.MAX_VALUE, id);
        }catch (Exception e) {
            e.printStackTrace();
            log.error("project delete change   redis remove  task queue error, key: {}, value: {}", Constants.MIRROR_IMPORT_KEY, id, e);
        }
    }

}
